package org.example.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.common.R;
import org.example.domain.CarClient;
import org.example.domain.ClientVip;
import org.example.dto.CarClientDto;
import org.example.listener.ClientListener;
import org.example.mapper.ClientVipMapper;
import org.example.service.CarClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("carClient")
public class CarClientController {

    @Autowired
    private CarClientService carClientService;
    @Autowired
    private ClientVipMapper clientVipMapper;
    @RequestMapping("carClientList")
    public R carClientList(CarClientDto carClientDto){
        PageHelper.startPage(carClientDto.getPageNum(), carClientDto.getPageSize());
        List<CarClient> list = carClientService.carClientList(carClientDto);
        PageInfo<CarClient> info = new PageInfo<>(list);
        return R.successed(info);
    }

    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        List<CarClient> list = carClientService.list();

        String simpleName = CarClient.class.getSimpleName();
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        simpleName = URLEncoder.encode(simpleName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + simpleName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), CarClient.class).sheet("模板").doWrite(list);
    }

    @PostMapping("upload")
    @ResponseBody
    public Map<String,Object> upload(MultipartFile file) throws IOException {
        Map<String,Object> map = new HashMap<>();
        try{
            map.put("code",200);
            EasyExcel.read(file.getInputStream(), CarClient.class, new ClientListener(carClientService)).sheet().doRead();
        }catch (IOException e){
            map.put("code",500);
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("save")
    public R saveCarClient(CarClient carClient){
        carClientService.saveClient(carClient);
        return R.successed(null);
    }

    @RequestMapping("vipList")
    public R vipList(){
        List<ClientVip> list = clientVipMapper.vipList();
        return R.successed(list);
    }

    @RequestMapping("clientDel")
    public R del(Integer clientId){
        carClientService.del(clientId);
        return R.successed(null);
    }
}
